package financial.sharing.center.system.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.EnableStatusEnum;
import financial.sharing.center.common.enums.ResponseContext;
import financial.sharing.center.common.utils.StringUtil;
import financial.sharing.center.system.dao.CompanyAccountMapper;
import financial.sharing.center.system.model.entity.CompanyAccount;
import financial.sharing.center.system.service.CompanyAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName CompanyAccountServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/5/6
 * @Version 1.0
 */
@Service
public class CompanyAccountServiceImpl implements CompanyAccountService {

    @Autowired
    private CompanyAccountMapper companyAccountMapper;

    private CompanyAccount getCompanyAccount(Integer id){
        return this.companyAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<CompanyAccount> getAllCompanyAccount(String selectCompanyName) {
        Example example = new Example(CompanyAccount.class);
        example.createCriteria().andLike("companyName", StringUtil.toLike(selectCompanyName))
                                .andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        List<CompanyAccount> companyAccountList = this.companyAccountMapper.selectByExample(example);
        return new PageInfo<>(companyAccountList);
    }

    private CompanyAccount getCompanyAccount(String invoiceTaxNumber){
        Example example = new Example(CompanyAccount.class);
        example.createCriteria().andEqualTo("invoiceTaxNumber", invoiceTaxNumber)
                                .andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        return this.companyAccountMapper.selectOneByExample(example);
    }

    @Override
    public Integer createOneCompanyAccount(String companyName, String invoiceTaxNumber,
                                           String invoiceAddress, String invoicePhone,
                                           String invoiceDepositBank, String invoiceBankAccount) {
        if (this.getCompanyAccount(invoiceTaxNumber) != null){
            return ResponseContext.已经存在.getValue();
        }
        CompanyAccount companyAccount = new CompanyAccount(companyName,invoiceTaxNumber,
                invoiceAddress,invoicePhone,invoiceDepositBank,invoiceBankAccount);
        int i = this.companyAccountMapper.insert(companyAccount);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.创建失败.getValue();
    }

    @Override
    public Integer compileOneCompanyAccount(CompanyAccount companyAccount) {
        if (this.getCompanyAccount(companyAccount.getInvoiceTaxNumber()) != null){
            return ResponseContext.已经存在.getValue();
        }
        int i = this.companyAccountMapper.updateByPrimaryKey(companyAccount);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public Integer deleteOneCompanyAccount(Integer id) {
        CompanyAccount companyAccount = this.getCompanyAccount(id);
        companyAccount.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.companyAccountMapper.updateByPrimaryKey(companyAccount);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }

    @Override
    public List<CompanyAccount> getListCompanyAccount() {
        Example example = new Example(CompanyAccount.class);
        example.createCriteria().andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        List<CompanyAccount> companyAccountList = this.companyAccountMapper.selectByExample(example);
        return companyAccountList;
    }
}
